﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HREDU.Hoang_Tuan.Classes;

namespace HREDU.Hoang_Tuan
{
    public partial class frmTimKiem : Form
     {
     public Form fCall = null;
        public string name = "";
     public frmTimKiem(Form frm)
     {
        
         // This call is required by the Windows Form Designer.
         InitializeComponent();
        
         // Add any initialization after the InitializeComponent() call.
                  fCall = frm;
         if (fCall.Name == "frmDONVI") {
             name = "Đơn vị";
             this.Text = "Tìm " + name;
             GroupBox2.Text = "Tên " + name + " cần tìm (Nhấn Enter để tìm)";
         }
         else if (fCall.Name == "frmDIADANH")
         {
             name = "Tỉnh thành";
             this.Text = "Tìm " + name;
             GroupBox2.Text = "Tên " + name + " cần tìm (Nhấn Enter để tìm)";
         }
     }
    
     private void txtTenTim_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
     {
         if (e.KeyCode == Keys.Enter) {
             LoadListBox(txtTenTim.Text);
         }
     }
    
     public void LoadListBox(string tenCanTim)
     {
         lvKQ.Items.Clear();
         if (fCall.Name == "frmDONVI") {
             DataTable db = CDONVICollection.TimTheoTen_DataTable(tenCanTim, "");
             GroupBox1.Text = "Kết quả tìm kiếm (" + db.Rows.Count.ToString() + name + ")";
             ListViewItem item;
             for(int i = 0; i < db.Rows.Count; i++){
                 item = new ListViewItem(db.Rows[i]["tenDonVi"].ToString());
                 if ((bool)db.Rows[i]["cuoi"] == false)
                 {
                     item.ImageIndex = 0;
                 }
                 else {
                     item.ImageIndex = 1;
                 }
                 item.Name = db.Rows[i]["maDonVi"].ToString();
                 lvKQ.Items.Add(item);
             }
         }
         else if (fCall.Name == "frmDIADANH")
         {
             DataTable db = CDIADANHCollection.TimTheoTen_DataTable(tenCanTim, "");
             GroupBox1.Text = "Kết quả tìm kiếm (" + db.Rows.Count.ToString() + name + ")";
             ListViewItem item;
             for (int i = 0; i < db.Rows.Count; i++)
             {
                 item = new ListViewItem(db.Rows[i]["tenDiaDanh"].ToString());
                 if ((bool)db.Rows[i]["cuoi"] == false)
                 {
                     item.ImageIndex = 0;
                 }
                 else
                 {
                     item.ImageIndex = 1;
                 }
                 item.Name = db.Rows[i]["maDiaDanh"].ToString();
                 lvKQ.Items.Add(item);
             }
         }
        
     }
    
     private void frmTimBanKho_Load(object sender, System.EventArgs e)
     {
         lvKQ.SmallImageList = ImageList1;
         lvKQ.Columns.Add("", lvKQ.Width, HorizontalAlignment.Left);
     }
    
     private void lvKQ_SelectedIndexChanged(object sender, System.EventArgs e)
     {
         if (lvKQ.SelectedItems.Count > 0)
         {
             if (fCall.Name == "frmDONVI")
             {
                 ((frmDONVI)fCall).HienThiGiaTriTimKiem(lvKQ.SelectedItems[0].Name);
             }
             else if (fCall.Name == "frmDIADANH")
             {
                 ((frmDIADANH)fCall).HienThiGiaTriTimKiem(lvKQ.SelectedItems[0].Name);
             }
         }
     }
 }
}

 